package com.xpgk.mvc.statistic.toShopStatistic;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Record;
import com.xpgk.constant.ConstantSplitPage;
import com.xpgk.dto.SplitPage;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerProductSurplus;
import com.xpgk.mvc.btoShop.BtoShop;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.statistic.customerInfo.CustomerInfoStatisticXLS;
import com.xpgk.mvc.statistic.trainingStatistic.TrainingStatisticSQL;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolDateTime;

public class ToShopStatisticController extends  BaseController{
	
	public  void  index()
	{	
		Organization  organ=Organization.dao.findById(getCUserOrganIds());
		Organization  parentOrg=Organization.dao.findById(organ.getStr(Organization.column_parentIds));
		setAttr("startTime", ToolDateTime.getMonthOneDay(new Date()));
		setAttr("organization", organ);
		setAttr("parentOrgan", parentOrg);
		render("toShopStatistic.html");
	}
	
	/**获取转入顾客信息
	 * 
	 */
	public  void  getIntoShop()
	{
		String json = paging(splitPage, ToShopStatisticSQL.sqlId_splitPageSelect, ToShopStatisticSQL.sqlId_splitPageIntoShopFrom);
		renderJson(json);
	}
	/**获取转出顾客信息
	 * 
	 */
	public  void  getOutShop()
	{
		String json = paging(splitPage, ToShopStatisticSQL.sqlId_splitPageSelect, ToShopStatisticSQL.sqlId_splitPageOutShopFrom);
		renderJson(json);
	}
	
	
	/**顾客转入导出
	 * 
	 */
	public void intoexcel()
	{	
		SplitPage  splitPage=new SplitPage();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startTime", getPara("startTime"));
		param.put("endTime", getPara("endTime"));
		param.put("organIds", getPara("organIds"));
		param.put("outOrganIds", getPara("outorganIds"));
		param.put("cushybh", getPara("cushybh"));
		param.put("cusname", getPara("cusname"));
		param.put("cusphone", getPara("cusphone"));
		param.put("toShopReason", getPara("zdyy_select").equals("-1")?"":getPara("zdyy_select"));
		splitPage.setPageSize(ConstantSplitPage.default_maxSize);
		splitPage.setQueryParam(param);
		String json = paging(splitPage, ToShopStatisticSQL.sqlId_splitPageSelect, ToShopStatisticSQL.sqlId_splitPageIntoShopFrom);
		List<Record>  list=(List<Record>)splitPage.getList();
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i).get("isconfirm").equals("1"))
			{
				list.get(i).set("isconfirm", "是");
			}else
			{
				list.get(i).set("isconfirm", "否");
			}
		}
		//搜索条件
		Organization  organ=Organization.dao.findById(getPara("organIds"));
		Organization  outorgan=Organization.dao.findById(getPara("outorganIds"));
		Code reason=Code.dao.findById(getPara("zdyy_select"));
		
		String  outorganstr=outorgan==null?"":("，转出门店："+outorgan.getStr(Organization.column_name));
		String  cushybhstr=getPara("cushybh").equals("")?"":("，顾客编号："+getPara("cushybh"));
		String  cusnamestr=getPara("cusname").equals("")?"":("，顾客姓名："+getPara("cusname"));
		String  cusphonestr=getPara("cusphone").equals("")?"":("，顾客手机："+getPara("cusphone"));
		String  reasonstr=reason==null?"":("，转店原因："+reason.getStr(Code.column_name));
		String search="日期："+getPara("startTime")+"~"+getPara("endTime")+"，当前门店："+organ.getStr(Organization.column_name)+
				outorganstr+cushybhstr+cusnamestr+cusphonestr+reasonstr+"，导出人员："+getCUser().getStr(User.column_name);
		render(new ToShopStatisticXLS(list,search,"1","顾客转入查询表"));
	}
	
	/**顾客转出导出
	 * 
	 */
	public void  outexcel()
	{	
		SplitPage  splitPage=new SplitPage();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startTime", getPara("startTime"));
		param.put("endTime", getPara("endTime"));
		param.put("organIds", getPara("organIds"));
		param.put("intoOrganIds", getPara("intoorganIds"));
		param.put("cushybh", getPara("cushybh"));
		param.put("cusname", getPara("cusname"));
		param.put("cusphone", getPara("cusphone"));
		param.put("toShopReason", getPara("zdyy_select").equals("-1")?"":getPara("zdyy_select").equals("-1"));
		splitPage.setPageSize(ConstantSplitPage.default_maxSize);
		splitPage.setQueryParam(param);
		String json = paging(splitPage, ToShopStatisticSQL.sqlId_splitPageSelect, ToShopStatisticSQL.sqlId_splitPageOutShopFrom);
		List<Record>  list=(List<Record>)splitPage.getList();
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i).get("isconfirm").equals("1"))
			{
				list.get(i).set("isconfirm", "是");
			}else
			{
				list.get(i).set("isconfirm", "否");
			}
		}
		
		//搜索条件
		Organization  organ=Organization.dao.findById(getPara("organIds"));
		Organization  intoorgan=Organization.dao.findById(getPara("intoorganIds"));
		Code reason=Code.dao.findById(getPara("zdyy_select"));
		
		String  intoorganstr=intoorgan==null?"":("，转入门店："+intoorgan.getStr(Organization.column_name));
		String  cushybhstr=getPara("cushybh").equals("")?"":("，顾客编号："+getPara("cushybh"));
		String  cusnamestr=getPara("cusname").equals("")?"":("，顾客姓名："+getPara("cusname"));
		String  cusphonestr=getPara("cusphone").equals("")?"":("，顾客手机："+getPara("cusphone"));
		String  reasonstr=reason==null?"":("，转店原因："+reason.getStr(Code.column_name));
		String search="日期："+getPara("startTime")+"~"+getPara("endTime")+"，当前门店："+organ.getStr(Organization.column_name)+
				intoorganstr+cushybhstr+cusnamestr+cusphonestr+reasonstr+"，导出人员："+getCUser().getStr(User.column_name);
		render(new ToShopStatisticXLS(list,search,"0","顾客转出查询表"));
	}
}
